package com.blb.day3;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 阻塞队列版包子店
 */
public class BaoziShop2 {

    //阻塞队列
    private BlockingQueue<Baozi> baozis = new LinkedBlockingQueue<>(100);

    //做包子
    public void makeBaozi(){
        Baozi baozi = new Baozi(baozis.size());
        try {
            //添加数据，满后自动阻塞，不满会自动通知等待线程
            baozis.put(baozi);
            System.out.println("生产者"+Thread.currentThread().getName()+"做了"+baozi);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //卖包子
    public void sellBaozi(){
        try {
            //删除列头的包子，如果仓库空了，让消费者线程等待
            Baozi baozi = baozis.take();
            System.out.println("消费者"+Thread.currentThread().getName()+"吃了"+baozi);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
